<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>classdef.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_classdef_classdef.js.html">classdef.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Restricted class definitions for Closure.
<a name="line17"></a> *
<a name="line18"></a> * @author johnlenz@google.com (John Lenz)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.labs.classdef&#39;);
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>/** @typedef {
<a name="line25"></a>     {constructor:!Function}|
<a name="line26"></a>     {constructor:!Function, statics:(Object|function(Function):void)}} */
<a name="line27"></a>goog.labs.classdef.ClassDescriptor;
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Creates a restricted form of a Closure &quot;class&quot;:
<a name="line32"></a> *   - from the compiler&#39;s perspective, the instance returned from the
<a name="line33"></a> *     constructor is sealed (no new properties may be added).  This enables
<a name="line34"></a> *     better type checking.
<a name="line35"></a> *   - the compiler will rewrite this definition to a form that is optimal
<a name="line36"></a> *     for type checking and optimization (initially this will be a more
<a name="line37"></a> *     traditional form).
<a name="line38"></a> *
<a name="line39"></a> * @param {Function} superClass The superclass or null.
<a name="line40"></a> * @param {goog.labs.classdef.ClassDescriptor} def
<a name="line41"></a> *     An object literal describing the
<a name="line42"></a> *     the class.  It may have the following properties:
<a name="line43"></a> *     &quot;constructor&quot;: the constructor function
<a name="line44"></a> *     &quot;statics&quot;: an object literal containing methods to add to the constructor
<a name="line45"></a> *        as &quot;static&quot; methods or a function that will recieve the constructor
<a name="line46"></a> *        function as its only parameter to which static properties can
<a name="line47"></a> *        be added.
<a name="line48"></a> *     all other properties are added to the prototype.
<a name="line49"></a> * @return {!Function} The class constructor.
<a name="line50"></a> */
<a name="line51"></a>goog.labs.classdef.defineClass = function(superClass, def) {
<a name="line52"></a>  // TODO(johnlenz): consider making the superClass an optional parameter.
<a name="line53"></a>  var constructor = def.constructor;
<a name="line54"></a>  var statics = def.statics;
<a name="line55"></a>  // Wrap the constructor prior to setting up the prototype and static methods.
<a name="line56"></a>  if (!constructor || constructor == Object.prototype.constructor) {
<a name="line57"></a>    throw Error(&#39;constructor property is required.&#39;);
<a name="line58"></a>  }
<a name="line59"></a>
<a name="line60"></a>  var cls = goog.labs.classdef.createSealingConstructor_(constructor);
<a name="line61"></a>  if (superClass) {
<a name="line62"></a>    goog.inherits(cls, superClass);
<a name="line63"></a>  }
<a name="line64"></a>
<a name="line65"></a>  // Remove all the properties that should not be copied to the prototype.
<a name="line66"></a>  delete def.constructor;
<a name="line67"></a>  delete def.statics;
<a name="line68"></a>
<a name="line69"></a>  goog.labs.classdef.applyProperties_(cls.prototype, def);
<a name="line70"></a>  if (statics != null) {
<a name="line71"></a>    if (statics instanceof Function) {
<a name="line72"></a>      statics(cls);
<a name="line73"></a>    } else {
<a name="line74"></a>      goog.labs.classdef.applyProperties_(cls, statics);
<a name="line75"></a>    }
<a name="line76"></a>  }
<a name="line77"></a>
<a name="line78"></a>  return cls;
<a name="line79"></a>};
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * @define {boolean} Whether the instances returned by
<a name="line84"></a> * goog.labs.classdef.defineClass should be sealed when possible.
<a name="line85"></a> */
<a name="line86"></a>goog.labs.classdef.SEAL_CLASS_INSTANCES = goog.DEBUG;
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * If goog.labs.classdef.SEAL_CLASS_INSTANCES is enabled and Object.seal is
<a name="line91"></a> * defined, this function will wrap the constructor in a function that seals the
<a name="line92"></a> * results of the provided constructor function.
<a name="line93"></a> *
<a name="line94"></a> * @param {!Function} ctr The constructor whose results maybe be sealed.
<a name="line95"></a> * @return {!Function} The replacement constructor.
<a name="line96"></a> * @private
<a name="line97"></a> */
<a name="line98"></a>goog.labs.classdef.createSealingConstructor_ = function(ctr) {
<a name="line99"></a>  if (goog.labs.classdef.SEAL_CLASS_INSTANCES &amp;&amp;
<a name="line100"></a>      Object.seal instanceof Function) {
<a name="line101"></a>    /** @this {*} */
<a name="line102"></a>    var wrappedCtr = function() {
<a name="line103"></a>      // Don&#39;t seal an instance of a subclass when it calls the constructor of
<a name="line104"></a>      // its super class as there is most likely still setup to do.
<a name="line105"></a>      var instance = ctr.apply(this, arguments) || this;
<a name="line106"></a>      if (this.constructor === wrappedCtr) {
<a name="line107"></a>        Object.seal(instance);
<a name="line108"></a>      }
<a name="line109"></a>      return instance;
<a name="line110"></a>    };
<a name="line111"></a>    return wrappedCtr;
<a name="line112"></a>  }
<a name="line113"></a>  return ctr;
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>// TODO(johnlenz): share these values with the goog.object
<a name="line118"></a>/**
<a name="line119"></a> * The names of the fields that are defined on Object.prototype.
<a name="line120"></a> * @type {!Array.&lt;string&gt;}
<a name="line121"></a> * @private
<a name="line122"></a> * @const
<a name="line123"></a> */
<a name="line124"></a>goog.labs.classdef.OBJECT_PROTOTYPE_FIELDS_ = [
<a name="line125"></a>  &#39;constructor&#39;,
<a name="line126"></a>  &#39;hasOwnProperty&#39;,
<a name="line127"></a>  &#39;isPrototypeOf&#39;,
<a name="line128"></a>  &#39;propertyIsEnumerable&#39;,
<a name="line129"></a>  &#39;toLocaleString&#39;,
<a name="line130"></a>  &#39;toString&#39;,
<a name="line131"></a>  &#39;valueOf&#39;
<a name="line132"></a>];
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>// TODO(johnlenz): share this function with the goog.object
<a name="line136"></a>/**
<a name="line137"></a> * @param {!Object} target The object to add properties to.
<a name="line138"></a> * @param {!Object} source The object to copy properites from.
<a name="line139"></a> * @private
<a name="line140"></a> */
<a name="line141"></a>goog.labs.classdef.applyProperties_ = function(target, source) {
<a name="line142"></a>  // TODO(johnlenz): update this to support ES5 getters/setters
<a name="line143"></a>
<a name="line144"></a>  var key;
<a name="line145"></a>  for (key in source) {
<a name="line146"></a>    if (Object.prototype.hasOwnProperty.call(source, key)) {
<a name="line147"></a>      target[key] = source[key];
<a name="line148"></a>    }
<a name="line149"></a>  }
<a name="line150"></a>
<a name="line151"></a>  // For IE the for-in-loop does not contain any properties that are not
<a name="line152"></a>  // enumerable on the prototype object (for example isPrototypeOf from
<a name="line153"></a>  // Object.prototype) and it will also not include &#39;replace&#39; on objects that
<a name="line154"></a>  // extend String and change &#39;replace&#39; (not that it is common for anyone to
<a name="line155"></a>  // extend anything except Object).
<a name="line156"></a>  for (var i = 0; i &lt; goog.labs.classdef.OBJECT_PROTOTYPE_FIELDS_.length; i++) {
<a name="line157"></a>    key = goog.labs.classdef.OBJECT_PROTOTYPE_FIELDS_[i];
<a name="line158"></a>    if (Object.prototype.hasOwnProperty.call(source, key)) {
<a name="line159"></a>      target[key] = source[key];
<a name="line160"></a>    }
<a name="line161"></a>  }
<a name="line162"></a>};
</pre>


</body>
</html>
